<template>
  <div v-loading="delLoading">
    <Search :search="search" :reset="reset" :inline="true">
      <template v-slot:body>
        <el-form-item label="采购入库单" prop="warehosnumber" maxlength="5">
          <el-input v-model="query.warehosnumber" name="warehosnumber" input-style="{width:100px}" clearable />
        </el-form-item>
        <el-form-item label="采购订单号" prop="fbillno" maxlength="5">
          <el-input v-model="query.fbillno" name="fbillno" input-style="{width:100px}" clearable />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="query.remarks" name="remarks" clearable />
        </el-form-item>
        <el-form-item label="入库类型" prop="incomingtype">
          <el-select v-model="query.incomingtype" placeholder="请选择入库类型" clearable>
            <el-option label="组织外入库" value="0" />
            <el-option label="组织间入库" value="1" />
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="单据状态" prop="statestate">
          <el-select v-model="query.state" placeholder="请选择单据状态" clearable>
            <el-option label="未审核" value="0" />
            <el-option label="已审核" value="1" />
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="回写状态" prop="writestate">
          <el-select v-model="query.writestate" placeholder="请选择回写状态" clearable>
            <el-option label="未回写" value="0" />
            <el-option label="回写成功" value="1" />
            <el-option label="回写失败" value="2" />
            <el-option label="全部" value="" />
          </el-select>
        </el-form-item>
      </template>
    </Search>
    <div class="table-default">
      <el-table :data="tableData" class="mt-3" v-loading="loading" border>
        <!-- <el-table-column prop="fseq" label="行号" /> -->
        <el-table-column prop="warehosnumber" label="采购入库单号" min-width="150" />
        <el-table-column prop="fbillno" label="采购订单单号" min-width="150" />
        <el-table-column prop="incomingtype" label="入库类型"  min-width="100">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.incomingtype == '组织间入库'">{{ scope.row.incomingtype }}</el-tag>
            <el-tag class="ml-2" type="primary" v-else>{{ scope.row.incomingtype }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remarks" label="备注" />
        <el-table-column prop="state" label="状态" >
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.state == '未审核' && scope.row.incomingtype != '组织间入库'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.state == '已审核' && scope.row.incomingtype != '组织间入库'">{{ scope.row.state }}</el-tag>
            <el-tag class="ml-2" type="info" v-if="scope.row.incomingtype == '组织间入库'">无需审核</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="writestate" label="回写状态">
          <template #default="scope">
            <el-tag class="ml-2" type="warning" v-if="scope.row.writestate == '回写失败'">{{ scope.row.writestate }}</el-tag>
            <el-tag class="ml-2" type="success" v-if="scope.row.writestate == '回写成功'">{{ scope.row.writestate }}</el-tag>
            <!-- <el-tag class="ml-2" type="info" v-if="scope.row.writestate == ''"></el-tag> -->
          </template>
        </el-table-column>
        <!-- <el-table-column prop="creator" label="创建者" width="130" /> -->
        <el-table-column prop="modifier" label="修改者" />
        <el-table-column prop="created_at" label="入库时间" min-width="100"/>
        <el-table-column label="操作" fixed="right" min-width="180">
          <template #default="scope">
            <!-- {{JSON.stringify(scope.row)}} -->
            <div class="btn-box">
              <router-link :to="{ path: '/purchasing/purchasewarehousDetail', query: { id: `${scope.row.id}`, state: `${scope.row.state}` } }">
                <Show />
              </router-link>
              <el-button
                type="warning"
                size="small"
                @click="approveHandler(scope.row.id)"
                v-if="scope.row.state != '已审核' && scope.row.incomingtype == '组织外入库'"
                v-loading.fullscreen.lock="fullscreenLoading"
                ><Icon name="wrench-screwdriver" :size="16" />审核</el-button
              >
              <Destroy @click="destroy(api, scope.row.id)" style="margin-left: 0" v-if="scope.row.incomingtype != '组织间入库' && scope.row.state != '已审核'" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Paginate />
    </div>
    <Dialog v-model="visible" :title="title" destroy-on-close>
      <Create @close="close(reset)" :primary="id" :api="api" />
    </Dialog>
  </div>
</template>

<script lang="ts" setup>
import { computed, onMounted, toRaw, ref, nextTick } from 'vue'
import { useRouter } from 'vue-router'
import { useGetList } from '/admin/composables/curd/useGetList'
import { useDestroy } from '/admin/composables/curd/useDestroy'
import { useOpen } from '/admin/composables/curd/useOpen'
import http from '/admin/support/http'
import { ElMessage } from 'element-plus'
import { setTableHeight } from '/admin/utils/setHeight.js'
import emitter from '/admin/utils/emitter'

let router = useRouter()

const api = 'purchasing/purchasewarehous'

const { data, query, search, reset, loading, toraw } = useGetList(api)
const { destroy, deleted, delLoading } = useDestroy()
const { open, close, title, visible, id } = useOpen()

const tableData = computed(() => data.value?.data)

console.log(tableData)
query.value.state = '0'
query.value.incomingtype = '0'

const writestateFormatter = {
  0: '回写失败',
  1: '回写成功',
}

const fullscreenLoading = ref(false)

const approveHandler = id => {
  fullscreenLoading.value = true

  http
    .put(`/purchasing/purchaseexamine/${id}`)
    .then(r => {
      fullscreenLoading.value = false
      ElMessage({
        message: r.data.message,
        type: 'success',
      })
      search()
    })
    .catch(error => {
      fullscreenLoading.value = false
    })
}

onMounted(() => {
  emitter.on('changepurchasewarehousStatus', val => {
    if (val.status == true) {
      search()
    }
  })
  search()
  deleted(reset)
  setTableHeight('.table-default')
})
</script>
<style>
.el-table {
  height: 87%;
}
.btn-box {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}
.el-button--small {
  margin-left: 0;
}
</style>
